package com.cn.wanxi.servlet;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 对应的是home主页
 * 下载tomcat容器
 */
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        怎么得到dao层的数据？
//        CompanyDao companyDao=new CompanyDao();
//        companyDao.logo
//        为什么要分层？
//        低耦合，高内聚：各干各的事情，面向对象
        /**
         * 作用域
         * page
         * request
         * session
         * application
         */
//        req.setAttribute("logo","../img/logo.jpg");
//        req.setAttribute("phone","111111122");
//        CompanyModel companyModel=new CompanyModel();
        CommonServlet.setCompanyAndNavData(req);
//        ==============================
//        1.调用服务逻辑层，得到对象数据
        NewsService newsService = new NewsService();
        NewsModel[] newsModels = newsService.getNewsModels();
//        2.将得到的数据保存到req容器里面
        req.setAttribute("newsModels", newsModels);
//        3.将数据转发到页面
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
    }
}
